import * as echarts from 'echarts';
// 直方图

// 计算直方图数据
function calculateHistogram(rawData, key ,binCount = 20) {
    let data = rawData.map(item => item[key]);

    let min = Math.min(...data);
    let max = Math.max(...data);
    let binWidth = (max - min) / binCount;
    
    let bins = new Array(binCount).fill(0);
    let binsMap = new Map();
    
    rawData.forEach(item => {
        const binIndex = Math.min(Math.floor((item[key] - min) / binWidth), binCount - 1);
        bins[binIndex]++;

        if (binsMap.has(binIndex)) {
            let list = binsMap.get(binIndex);
            list.push(item);
            binsMap.set(binIndex, list);
        } else {
            binsMap.set(binIndex, [item]);
        }
    });

    let lastItem = rawData[rawData.length - 1];
    let xAxis = Math.min(Math.floor((lastItem[key] - min) / binWidth), binCount - 1)
    
    return {
        bins: bins,
        binsMap: binsMap,
        intervals: Array.from({length: binCount}, (_, i) => {
            const start = min + i * binWidth;
            const end = min + (i + 1) * binWidth;
            return `${start.toFixed(1)} - ${end.toFixed(1)}`;
        }),
        markPoint: [{
            name : "curr",
            value : "curr",
            xAxis: xAxis,
            yAxis:bins[xAxis], 
        }]
    };
}

export function buildData(data, key, binCount = 20) {
    let histogramData = calculateHistogram(data, key, binCount);
    
    let option = {
        myBinsMap: histogramData.binsMap,
        title: {
            text: '数据分布直方图',
            subtext: `数据量: ${data.length} | 区间数: ${binCount}`,
            left: 'center'
        },
        tooltip: {
            trigger: 'axis',
            formatter: function(params) {
                return `${params[0].name}<br/>频数: ${params[0].value}`;
            }
        },
        xAxis: {
            type: 'category',
            data: histogramData.intervals,
            name: '数值区间',
            axisLabel: {
                rotate: 45
            }
        },
        yAxis: {
            type: 'value',
            name: '频数'
        },
        series: [
            {
                name: '频数',
                type: 'bar',
                data: histogramData.bins,
                markPoint: {
                    data: histogramData.markPoint
                },
                itemStyle: {
                    color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                        { offset: 0, color: '#5470c6' },
                        { offset: 1, color: '#91cc75' }
                    ])
                },
                emphasis: {
                    itemStyle: {
                        color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                            { offset: 0, color: '#fac858' },
                            { offset: 1, color: '#ee6666' }
                        ])
                    }
                }
            }
        ],
        dataZoom: [
            {
                type: 'inside',
                xAxisIndex: 0,
                start: 0,
                end: 100
            },
            {
                type: 'slider',
                xAxisIndex: 0,
                start: 0,
                end: 100
            }
        ],
        grid: {
            left: '3%',
            right: '4%',
            bottom: '15%',
            containLabel: true
        }
    };

    return option;
};